/*
 *  Copyright (C) 2010 Glauco
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.samples;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.Serializable;
import javax.persistence.*;
import org.balisunrise.annotation.*;
import org.hibernate.annotations.Type;

/**
 *
 * @author Glauco
 */
@Entity
// <editor-fold defaultstate="collapsed" desc="VisualEntity">
@VisualEntity(transactions = {
    @Transaction(editor = "", search = "", title = "Cadastro de Estados")},
editors = {
    @Editor(panel = "", title = "Estado")},
searchs = {
    @Search(filter = "", grid = "", title = "Pesquisa de Estados")},
panels = {
    @Panel(title = "Estado"),
    @Panel(alias="selByUF",title="Estado"),
    @Panel(alias="sel",title="Estado")},
filters = {
    @Filter(alias = "",title="Filtro para Estados")},
grids = {
    @Grid(title="Estados"),
    @Grid(alias="embUF",title="Estados")})
// </editor-fold>
public class Estado implements Serializable{

    // <editor-fold defaultstate="collapsed" desc="PropertyChangeSupport">
    @Transient
    private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);

    public void addPropertyChangeListener(PropertyChangeListener listener) {
        propertyChangeSupport.addPropertyChangeListener(listener);
    }

    public void removePropertyChangeListener(PropertyChangeListener listener) {
        propertyChangeSupport.removePropertyChangeListener(listener);
    }
    public static final String PROP_ID = "id";
    public static final String PROP_NOME = "nome";
    public static final String PROP_UF = "uf";
    public static final String PROP_PAIS = "pais";
    public static final String PROP_IBGE = "ibge";
    // </editor-fold>
    
    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE)
    private Long id;

    // <editor-fold defaultstate="collapsed" desc="annotations">
    @Column(length = 50, columnDefinition = "char(50)",nullable=false)
    @Type(type="org.balisunrise.dao.hibernate.TrimmedString")
    @PanelField(definitions = {
        @FieldDefinition(label = "Estado"),
        @FieldDefinition(alias="selByUF",label="",index=1,readOnly=true,jumpNextRow=false)})
    @FilterCriteria(definitions = {
        @CriteriaDefinition(label = "Estado", filterType = FilterType.ILIKE)})
    @GridColumn(definitions = {
        @ColumnDefinition(alias={"","embUF"},name = "Estado")})// </editor-fold>
    private String nome;

    // <editor-fold defaultstate="collapsed" desc="annotations">
    @Column(length = 2, columnDefinition = "char(2)",nullable=false)
    @Type(type="org.balisunrise.dao.hibernate.TrimmedString")
    @Selection(aliasForFields={"selByUF","sel"},filterType=FilterType.EQUALS)
    @PanelField(definitions = {
        @FieldDefinition(label = "UF"),
        @FieldDefinition(alias={"selByUF"},label="UF",index=0),
        @FieldDefinition(alias={"sel"},label="UF",index=0,jumpNextRow=false)})
    @GridColumn(definitions = {
        @ColumnDefinition(index = 1, name = "UF", width = 20)})// </editor-fold>
    private String uf;

    // <editor-fold defaultstate="collapsed" desc="annotations">
    @ManyToOne(optional=false)
    @PanelField(selection = {
        @FieldSelection(notFoundMessage = "País não encontrado!",
        panel = "selection",
        search = "selection")},
        embedded={@FieldEmbedded(alias="selByUF",panel="emb",index=2)})
    @GridColumn(embedded = {
        @ColumnEmbedded(grid = "embedded", index = 2)})// </editor-fold>
    private Pais pais;

    // <editor-fold defaultstate="collapsed" desc="annotations">
    @Basic(optional=false)
    @PanelField(definitions = {
        @FieldDefinition(label = "Código IBGE")})
    @GridColumn(definitions = {
        @ColumnDefinition(index = 3, name = "IBGE", width = 50)})// </editor-fold>
    private Byte ibge;

    public Estado() { }

    // <editor-fold defaultstate="collapsed" desc="Gets & Sets">

    public Byte getIbge() {
        return ibge;
    }

    public void setIbge(Byte ibge) {

        if(ibge == null || ibge == 0)//.equals((Byte)0))
           throw new RuntimeException("Valor do IBGE deve ser preenchido!");

        Byte oldIbge = this.ibge;
        this.ibge = ibge;
        propertyChangeSupport.firePropertyChange(PROP_IBGE, oldIbge, ibge);
    }

    public Pais getPais() {
        return pais;
    }

    public void setPais(Pais pais) {

        if(pais == null)
            throw new RuntimeException("O País deve ser informado!");

        Pais oldPais = this.pais;
        this.pais = pais;
        propertyChangeSupport.firePropertyChange(PROP_PAIS, oldPais, pais);
    }

    public String getUf() {
        return uf;
    }

    public void setUf(String uf) {

        if(uf == null || uf.length() != 2)
            throw new RuntimeException("UF deve conter dois (2) dígitos!");

        String oldUf = this.uf;
        this.uf = uf;
        propertyChangeSupport.firePropertyChange(PROP_UF, oldUf, uf);
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {

        if(nome == null || nome.length() == 0)
            throw new RuntimeException("Nome do Estado deve ser informado!");

        String oldNome = this.nome;
        this.nome = nome;
        propertyChangeSupport.firePropertyChange(PROP_NOME, oldNome, nome);
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        Long oldId = this.id;
        this.id = id;
        propertyChangeSupport.firePropertyChange(PROP_ID, oldId, id);
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Estado other = (Estado) obj;
        if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 97 * hash + (this.id != null ? this.id.hashCode() : 0);
        return hash;
    }

    @Override
    public String toString() {
        return "Estado[" + "nome: " + nome + " uf: " + uf + ']';
    }

    // </editor-fold>
    
}
